function logout(){
	console.log("logout");
	eraseCookie("loginId");
	eraseCookie("loginName");
	eraseCookie("loginType");
	$("#dispCurrUser").hide();
	dispLoginView();
	$.Notify({
		content : "logout successfull",
		style : {
			background : 'coral'
		}
	});
}

function login() {
	
	$("#loginheader").text("Login");
	$("#loginheader").css('background-color', 'white');
	
	$.ajax({
		type : "POST",
		url : "/ComplaintReg/rest/service/login",
		data : $("#userlogin").serialize(),
		success : function(data) {
			// console.log(data);
			response = JSON.parse(data);

			if (response.login == "true" && response.loginType == "user") {
				$("#dispLoginId").text(response.loginName);
				$("#dispCurrUser").show();
				$.Notify({
					content : "login successfull",
					style : {
						background : 'coral'
					}
				});

				createCookie("loginId", response.id, "1");
				createCookie("loginName", response.loginName, "1");
				createCookie("loginType", response.loginType, "1");

				dispUserView();

			} else if (response.login == "true" && response.loginType == "dept") {

				$("#dispLoginId").text(response.loginName);
				$("#dispCurrUser").show();
				$.Notify({
					content : "login successfull",
					style : {
						background : 'coral'
					}
				});
				$("#loginform").hide();
				
				createCookie("loginId", response.id, "1");
				createCookie("loginName", response.loginName, "1");
				createCookie("loginType", response.loginType, "1");
				
				dispDeptView();

			}  else if (response.login == "true" && response.loginType == "admin") {

				$("#dispLoginId").text(response.loginName);
				$("#dispCurrUser").show();
				$.Notify({
					content : "login successfull",
					style : {
						background : 'coral'
					}
				});
				$("#loginform").hide();
				
				createCookie("loginId", response.id, "1");
				createCookie("loginName", response.loginName, "1");
				createCookie("loginType", response.loginType, "1");
				
				dispAdminView();

			} else {
				$("#loginheader").text("incorrect credentials");
				$("#loginheader").css('background-color', 'coral');
			}
		}
	});

}

function dispLoginView() {
	
	var loginType = readCookie("loginType");
	var loginName = readCookie("loginName");
	if(loginType == "user"){
		$("#dispLoginId").text(loginName);
		$("#dispCurrUser").show();
		console.log("dispUserView");
		dispUserView();
		return;
	}else if(loginType == "dept"){
		$("#dispLoginId").text(loginName);
		$("#dispCurrUser").show();
		
		dispDeptView();
		return;
	}else if(loginType == "admin"){
		$("#dispLoginId").text(loginName);
		$("#dispCurrUser").show();
		
		dispAdminView();
		return;
	}

	$("#b1txt").text("Login");
	$("#b2txt").text("Create User");
	$("#b3txt").text("Create Dept");
	//$("#b4txt").text("x");
	
	$("#SideBarButton4").hide();

	$("#SideBarButton1").off().click(function() {
		dispLogin();
	});
	$("#SideBarButton2").off().click(function() {
		dispCreateUser();
	});
	$("#SideBarButton3").off().click(function() {
		dispCreateDept();
	});
	$("#SideBarButton4").off().click(function() {

	});

	$("#dispCont .grid").hide();

	dispLogin();

}

function dispLogin() {

	$("#dispCont .grid").hide();

	$("#SideBarButton1").removeClass("info").addClass("warning");
	$("#SideBarButton2").removeClass("warning").addClass("info");
	$("#SideBarButton3").removeClass("warning").addClass("info");
	$("#SideBarButton4").removeClass("warning").addClass("info");

	$("#userlogin").off().submit(function() {
		login();
		return false;
	});

	$("#divLogin").slideDown();
}

function dispCreateUser() {

	$("#dispCont .grid").hide();

	$("#SideBarButton1").removeClass("warning").addClass("info");
	$("#SideBarButton2").removeClass("info").addClass("warning");
	$("#SideBarButton3").removeClass("warning").addClass("info");
	$("#SideBarButton4").removeClass("warning").addClass("info");

	$("#createUser").off().submit(
			function() {

				$.ajax({
					type : "POST",
					url : "/ComplaintReg/rest/service/createUser",
					data : $("#createUser").serialize(),
					success : function(data) {

						$.Dialog({
							overlay : true,
							shadow : true,
							flat : true,
							title : 'Flat window',
							content : '',
							padding : 10,
							onShow : function(_dialog) {

								var content = "<p>User id : <strong>" + data.uid + "</strong></p><p>Password : <strong>" + data.pwd
										+ "<strong></p>" + '<button class="button" type="button" id="ButtonCloseDialog">Login</button> ';

								$.Dialog.title("User created ");
								$.Dialog.content(content);
								$("#ButtonCloseDialog").click(function() {
									$.Dialog.close();
									dispLogin();
								});
								$.Metro.initInputs();
							}
						});

					}
				});

				return false;
			});

	$("#LoginCreateUser").slideDown();

	$("#datepicker").datepicker({
		date : "1980-01-01", // set init date
		format : "yyyy-mm-dd", // set output format
		effect : "none", // none, slide, fade
		position : "bottom", // top or bottom,
		locale : 'en', // 'ru' or 'en', default is $.Metro.currentLocale
	});

}

function dispCreateDept() {

	$("#dispCont .grid").hide();

	$("#SideBarButton1").removeClass("warning").addClass("info");
	$("#SideBarButton2").removeClass("warning").addClass("info");
	$("#SideBarButton3").removeClass("info").addClass("warning");
	$("#SideBarButton4").removeClass("warning").addClass("info");

	$("#createDept").off().submit(
			function() {

				$.ajax({
					type : "POST",
					url : "/ComplaintReg/rest/service/createDept",
					data : $("#createDept").serialize(),
					success : function(data) {

						$.Dialog({
							overlay : true,
							shadow : true,
							flat : true,
							title : 'Flat window',
							content : '',
							padding : 10,
							onShow : function(_dialog) {

								var content = "<p>Department id : <strong>" + data.did + "</strong></p><p>Password : <strong>" + data.dept_pwd
										+ "<strong></p>" + '<button class="button" type="button" id="ButtonCloseDialog">Login</button> ';

								$.Dialog.title("Dept created");
								$.Dialog.content(content);
								$("#ButtonCloseDialog").click(function() {
									$.Dialog.close();
									dispLogin();
								});
								$.Metro.initInputs();
							}
						});

					}
				});

				return false;
			});

	$("#LoginCreateDept").slideDown();

}

// ------------------------------------------------------------------ COOKIE
// HANDLERS -----------------------------------------------------//
function createCookie(name, value, days) {
	var expires;

	if (days) {
		var date = new Date();
		date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
		expires = "; expires=" + date.toGMTString();
	} else {
		expires = "";
	}
	document.cookie = escape(name) + "=" + escape(value) + expires + "; path=/";
}

function readCookie(name) {
	var nameEQ = escape(name) + "=";
	var ca = document.cookie.split(';');
	for (var i = 0; i < ca.length; i++) {
		var c = ca[i];
		while (c.charAt(0) === ' ')
			c = c.substring(1, c.length);
		if (c.indexOf(nameEQ) === 0)
			return unescape(c.substring(nameEQ.length, c.length));
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name, "", -1);
}
